package bb.lanxing.model.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Parcel;
import android.os.Parcelable;

import com.baidu.mapapi.model.LatLng;

import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.LitePal;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import bb.lanxing.model.WorkoutDatabaseHelper;
import bb.lanxing.test.PolylineUtil;
import bb.lanxing.util.BiCiCoorConverter;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.JsonUtil;

public class Trackpoint extends LitePalTableBase implements Serializable, Cloneable, Parcelable, ITrackPoint {
    final private static String TAG = "Trackpoint";
    public static final double BAIDU_MAX = 200.0d;
    private static String COLUMNS_ALL = "id, workoutid, latitude, longitude, altitude, speed, accuracy, bearing, time, cadence, heartrate, wheelrpm, sumcadence, sumwheel, source, flag, pressure, temperature, elevation, grade, power, powerext, distanceeach, durationeach, pointtype, speedgps, speedca, altitudegps, altitudeca, distance, duration, elevationgain, elevationloss";
    private static String COLUMNS_FOR_CHART = "id, altitude, speed, cadence, heartrate, power,latitude,longitude,sumwheel,time";
    private static String COLUMNS_FOR_MAP = "id, latitude, longitude, altitude, pointtype,speed,time";
    public static String COLUMNS_FOR_POWER = "id, time, power";
    private static String COLUMNS_FOR_UPLOAD = "accuracy, altitude, bearing, latitude, longitude, speed, time, id, workoutid, cadence, wheelrpm, heartrate, pressure, temperature, power, powerext, distanceeach, durationeach, pointtype, distance, duration, elevationgain, elevationloss";
    public static final Parcelable.Creator<Trackpoint> CREATOR = new Parcelable.Creator<Trackpoint>() {
        @Override
        public Trackpoint createFromParcel(Parcel source) {
            return new Trackpoint(source);
        }

        @Override
        public Trackpoint[] newArray(int size) {
            return new Trackpoint[size];
        }
    };
    public static final int FLAG_START = 0;
    public static final int FLAG_RUNNING = 1;
    public static final int FLAG_STOP = 2;
    public static final int FLAG_PAUSE = 3;
    public static final int POINT_TYPE_NORMAL = 0;
    public static final int POINT_TYPE_FINISH = 1;
    public static final int POINT_TYPE_CRASH = 2;
    private static SQLiteDatabase db;
    private double accuracy;
    private double altitude;
    private double altitudeCa;
    private double altitudeGps;
    private double bearing;
    private int cadence;
    private double distance;
    private double distanceEach;
    private long duration;
    private long durationEach;
    private double elevation;
    private double elevationGain;
    private double elevationLoss;
    private int flag;
    private double grade;
    private int heartrate;
    private double latitude;
    private double longitude;
    private int pointType;
    private double power;
    private String powerExt;
    private double pressure;
    private int source;
    private double speed;
    private double speedCa;
    private double speedGps;
    private int sumCadence;
    private int sumWheel;
    private double temperature;
    private long time;
    private int wheelRpm;
    private long workoutId;


    @Override
    public int describeContents() {
        return 0;
    }

    public Trackpoint() {
        this.latitude = 90.0d;
        this.longitude = 180.0d;
        this.source = 1;
        this.altitudeCa = -2.147483648E9d;
    }

    public Trackpoint(JSONObject jSONObject) throws JSONException {
        this.latitude = 90.0d;
        this.longitude = 180.0d;
        this.source = 1;
        this.altitudeCa = -2.147483648E9d;
        setAccuracy(JsonUtil.getDoubleValue("accuracy", jSONObject));
        setAltitude(JsonUtil.getDoubleValue("altitude", jSONObject));
        setLatitude(JsonUtil.getDoubleValue("latitude", jSONObject));
        setLongitude(JsonUtil.getDoubleValue("longitude", jSONObject));
        setBearing(JsonUtil.getDoubleValue("bearing", jSONObject));
        setSpeed(JsonUtil.getDoubleValue("speed", jSONObject));
        setTime(JsonUtil.getLongValue("time", jSONObject));
        setHeartrate(JsonUtil.getIntegerValue("heartrate", jSONObject));
        setWheelRpm(JsonUtil.getIntegerValue("wheelRpm", jSONObject));
        setCadence(JsonUtil.getIntegerValue("cadence", jSONObject));
        setWorkoutId(JsonUtil.getLongValue("workoutServerId", jSONObject));
        setPressure(JsonUtil.getDoubleValue("atm", jSONObject));
        setTemperature(JsonUtil.getDoubleValue("temperature", jSONObject));
        setPower(JsonUtil.getDoubleValue("power", jSONObject));
    }

    public static Trackpoint parseCursor(Cursor cursor) {
        return parseCursor(cursor, null);
    }

    public static Trackpoint parseCursor(Cursor cursor, Map<String, Integer> map) {
        Trackpoint trackpoint = new Trackpoint();
        int index = getIndex(cursor, map, "id");
        if (index >= 0) {
            trackpoint.id = cursor.getLong(index);
        }
        int index2 = getIndex(cursor, map, "workoutid");
        if (index2 >= 0) {
            trackpoint.workoutId = cursor.getLong(index2);
        }
        int index3 = getIndex(cursor, map, "latitude");
        if (index3 >= 0) {
            trackpoint.latitude = cursor.getDouble(index3);
        }
        int index4 = getIndex(cursor, map, "longitude");
        if (index4 >= 0) {
            trackpoint.longitude = cursor.getDouble(index4);
        }
        int index5 = getIndex(cursor, map, "altitude");
        if (index5 >= 0) {
            trackpoint.altitude = cursor.getDouble(index5);
        }
        int index6 = getIndex(cursor, map, "speed");
        if (index6 >= 0) {
            trackpoint.speed = cursor.getDouble(index6);
        }
        int index7 = getIndex(cursor, map, "accuracy");
        if (index7 >= 0) {
            trackpoint.accuracy = cursor.getDouble(index7);
        }
        int index8 = getIndex(cursor, map, "bearing");
        if (index8 >= 0) {
            trackpoint.bearing = cursor.getDouble(index8);
        }
        int index9 = getIndex(cursor, map, "time");
        if (index9 >= 0) {
            trackpoint.time = cursor.getLong(index9);
        }
        int index10 = getIndex(cursor, map, "cadence");
        if (index10 >= 0) {
            trackpoint.cadence = cursor.getInt(index10);
        }
        int index11 = getIndex(cursor, map, "heartrate");
        if (index11 >= 0) {
            trackpoint.heartrate = cursor.getInt(index11);
        }
        int index12 = getIndex(cursor, map, "wheelrpm");
        if (index12 >= 0) {
            trackpoint.wheelRpm = cursor.getInt(index12);
        }
        int index13 = getIndex(cursor, map, "sumcadence");
        if (index13 >= 0) {
            trackpoint.sumCadence = cursor.getInt(index13);
        }
        int index14 = getIndex(cursor, map, "sumwheel");
        if (index14 >= 0) {
            trackpoint.sumWheel = cursor.getInt(index14);
        }
        int index15 = getIndex(cursor, map, "source");
        if (index15 >= 0) {
            trackpoint.source = cursor.getInt(index15);
        }
        int index16 = getIndex(cursor, map, "flag");
        if (index16 >= 0) {
            trackpoint.flag = cursor.getInt(index16);
        }
        int index17 = getIndex(cursor, map, "pressure");
        if (index17 >= 0) {
            trackpoint.pressure = cursor.getDouble(index17);
        }
        int index18 = getIndex(cursor, map, "temperature");
        if (index18 >= 0) {
            trackpoint.temperature = cursor.getDouble(index18);
        }
        int index19 = getIndex(cursor, map, "elevation");
        if (index19 >= 0) {
            trackpoint.elevation = cursor.getDouble(index19);
        }
        int index20 = getIndex(cursor, map, "grade");
        if (index20 >= 0) {
            trackpoint.grade = cursor.getDouble(index20);
        }
        int index21 = getIndex(cursor, map, "power");
        if (index21 >= 0) {
            trackpoint.power = cursor.getDouble(index21);
        }
        int index22 = getIndex(cursor, map, "powerext");
        if (index22 >= 0) {
            trackpoint.powerExt = cursor.getString(index22);
        }
        int index23 = getIndex(cursor, map, "distanceeach");
        if (index23 >= 0) {
            trackpoint.distanceEach = cursor.getDouble(index23);
        }
        int index24 = getIndex(cursor, map, "durationeach");
        if (index24 >= 0) {
            trackpoint.durationEach = cursor.getLong(index24);
        }
        int index25 = getIndex(cursor, map, "pointtype");
        if (index25 >= 0) {
            trackpoint.pointType = cursor.getInt(index25);
        }
        int index26 = getIndex(cursor, map, "speedgps");
        if (index26 >= 0) {
            trackpoint.speedGps = cursor.getDouble(index26);
        }
        int index27 = getIndex(cursor, map, "speedca");
        if (index27 >= 0) {
            trackpoint.speedCa = cursor.getDouble(index27);
        }
        int index28 = getIndex(cursor, map, "altitudegps");
        if (index28 >= 0) {
            trackpoint.altitudeGps = cursor.getDouble(index28);
        }
        int index29 = getIndex(cursor, map, "altitudeca");
        if (index29 >= 0) {
            trackpoint.altitudeCa = cursor.getDouble(index29);
        }
        int index30 = getIndex(cursor, map, "distance");
        if (index30 >= 0) {
            trackpoint.distance = cursor.getDouble(index30);
        }
        int index31 = getIndex(cursor, map, "duration");
        if (index31 >= 0) {
            trackpoint.duration = cursor.getLong(index31);
        }
        int index32 = getIndex(cursor, map, "elevationgain");
        if (index32 >= 0) {
            trackpoint.elevationGain = cursor.getDouble(index32);
        }
        int index33 = getIndex(cursor, map, "elevationloss");
        if (index33 >= 0) {
            trackpoint.elevationLoss = cursor.getDouble(index33);
        }
        return trackpoint;
    }

    private static int getIndex(Cursor cursor, Map<String, Integer> map, String str) {
        if (map == null) {
            int columnIndex = cursor.getColumnIndex(str);
            if (columnIndex < 0) {
                columnIndex = cursor.getColumnIndex(str.toLowerCase());
            }
            return columnIndex < 0 ? cursor.getColumnIndex(str.toUpperCase()) : columnIndex;
        }
        Integer num = map.get(str);
        if (num != null) {
            return num;
        }
        return -1;
    }

    public ContentValues toContentValues() {
        ContentValues contentValues = new ContentValues();
        contentValues.put("id", this.id);
        contentValues.put("workoutid", this.workoutId);
        contentValues.put("latitude", this.latitude);
        contentValues.put("longitude", this.longitude);
        contentValues.put("altitude", this.altitude);
        contentValues.put("speed", this.speed);
        contentValues.put("accuracy", this.accuracy);
        contentValues.put("bearing", this.bearing);
        contentValues.put("time", this.time);
        contentValues.put("cadence", this.cadence);
        contentValues.put("heartrate", this.heartrate);
        contentValues.put("wheelrpm", this.wheelRpm);
        contentValues.put("sumcadence", this.sumCadence);
        contentValues.put("sumwheel", this.sumWheel);
        contentValues.put("source", this.source);
        contentValues.put("flag", this.flag);
        contentValues.put("pressure", this.pressure);
        contentValues.put("temperature", this.temperature);
        contentValues.put("elevation", this.elevation);
        contentValues.put("grade", this.grade);
        contentValues.put("power", this.power);
        String str = this.powerExt;
        if (str != null) {
            contentValues.put("powerext", str);
        }
        contentValues.put("distanceeach", this.distanceEach);
        contentValues.put("durationeach", this.durationEach);
        contentValues.put("pointtype", this.pointType);
        contentValues.put("speedgps", this.speedGps);
        contentValues.put("speedca", this.speedCa);
        contentValues.put("altitudegps", this.altitudeGps);
        contentValues.put("altitudeca", this.altitudeCa);
        contentValues.put("distance", this.distance);
        contentValues.put("duration", this.duration);
        contentValues.put("elevationGain", this.elevationGain);
        contentValues.put("elevationloss", this.elevationLoss);
        return contentValues;
    }

    public static List<Trackpoint> getUnUploadPoint(long workoutId, long offset, int localSource) {
        String str = localSource == 2 ? " order by sumWheel asc" : "";
        long countByWorkout = getCountByWorkout(workoutId);
        db = LitePal.getDatabase();//getSugarDb().getDB();
        LinkedList linkedList = new LinkedList();
        SQLiteDatabase sQLiteDatabase = db;
        Cursor rawQuery = sQLiteDatabase.rawQuery("SELECT " + COLUMNS_FOR_UPLOAD + " FROM trackpoint WHERE workout_id = ?" + str + " limit ? offset ?", new String[]{String.valueOf(workoutId), String.valueOf(countByWorkout - offset), String.valueOf(offset)});
        while (rawQuery.moveToNext()) {
            Trackpoint trackpoint = new Trackpoint();
            trackpoint.accuracy = rawQuery.getDouble(0);
            trackpoint.altitude = rawQuery.getDouble(1);
            trackpoint.bearing = rawQuery.getDouble(2);
            trackpoint.latitude = rawQuery.getDouble(3);
            trackpoint.longitude = rawQuery.getDouble(4);
            trackpoint.speed = rawQuery.getFloat(5);
            trackpoint.time = rawQuery.getLong(6);
            trackpoint.id = rawQuery.getLong(7);
            trackpoint.workoutId = rawQuery.getLong(8);
            trackpoint.cadence = rawQuery.getInt(9);
            trackpoint.wheelRpm = rawQuery.getInt(10);
            trackpoint.heartrate = rawQuery.getInt(11);
            trackpoint.pressure = rawQuery.getDouble(12);
            trackpoint.temperature = rawQuery.getDouble(13);
            trackpoint.power = rawQuery.getDouble(14);
            trackpoint.powerExt = rawQuery.getString(15);
            trackpoint.distanceEach = rawQuery.getDouble(16);
            trackpoint.durationEach = rawQuery.getLong(17);
            trackpoint.pointType = rawQuery.getInt(18);
            trackpoint.distance = rawQuery.getDouble(19);
            trackpoint.duration = rawQuery.getLong(20);
            trackpoint.elevationGain = rawQuery.getDouble(21);
            trackpoint.elevationLoss = rawQuery.getDouble(22);
            linkedList.add(trackpoint);
        }
        rawQuery.close();
        return linkedList;
    }

    public static String getPolyline(long workoutId, double max) {
        List<ITrackPoint> byWorkoutForMap = getByWorkoutForMap(workoutId);
        int size = byWorkoutForMap.size();
        if (size == 0) {
            return null;
        }
        int ceil = (int) Math.ceil(size / max);
        ArrayList<LatLng> arrayList = new ArrayList<>();
        for (int i = 0; i < size; i += ceil) {
            ITrackPoint iTrackPoint = byWorkoutForMap.get(i);
            if (CommonUtil.isPointValid(iTrackPoint.getLatitude(), iTrackPoint.getLongitude())) {
                arrayList.add(new LatLng(iTrackPoint.getLatitude(), iTrackPoint.getLongitude()));
            }
        }
        if (ceil > 1) {
            ITrackPoint iTrackPoint2 = byWorkoutForMap.get(size - 1);
            if (CommonUtil.isPointValid(iTrackPoint2.getLatitude(), iTrackPoint2.getLongitude())) {
                arrayList.add(new LatLng(iTrackPoint2.getLatitude(), iTrackPoint2.getLongitude()));
            }
        }
        return PolylineUtil.encode(BiCiCoorConverter.earth2Common(arrayList));
    }

    public static void deleteByWorkout(long workoutId) {

        String strWorkoutId = String.valueOf(workoutId);
        LitePal.deleteAll(Trackpoint.class, "workoutId=?", strWorkoutId);

//        deleteAll(Trackpoint.class, " workout_id = " + j, new String[0]);

    }

    public static List<ITrackPoint> getByWorkoutForMap(long workoutId) {
        return getByWorkoutForMap(workoutId, 1);
    }

    public static List<Trackpoint> getByWorkoutId(long workoutId) {
        return LitePal.where("workoutId =? ", String.valueOf(workoutId)).find(Trackpoint.class);
    }

    public static List<ITrackPoint> getByWorkoutForMap(long workoutId, int localSource) {
        db = LitePal.getDatabase();//getSugarDb().getDB();
        String str = "SELECT " + COLUMNS_FOR_MAP + " FROM trackpoint WHERE workoutid = ?";
        if (localSource == 2) {
            str = str + " order by sumwheel asc";
        }
        Cursor rawQuery = db.rawQuery(str, new String[]{String.valueOf(workoutId)});
        int count = rawQuery.getCount();
        if (count < 0) {
            count = 0;
        }
        ArrayList<ITrackPoint> arrayList = new ArrayList<>(count);
        while (rawQuery.moveToNext()) {
            Trackpoint trackpoint = new Trackpoint();
            trackpoint.id = rawQuery.getLong(0);
            trackpoint.latitude = rawQuery.getDouble(1);
            trackpoint.longitude = rawQuery.getDouble(2);
            trackpoint.altitude = rawQuery.getDouble(3);
            trackpoint.pointType = rawQuery.getInt(4);
            trackpoint.speed = rawQuery.getDouble(5);
            trackpoint.time = rawQuery.getLong(6);
            if (CommonUtil.isPointValid(trackpoint)) {
                arrayList.add(trackpoint);
            }
        }
        rawQuery.close();
        return arrayList;
    }

    public static List<ITrackPoint> getByWorkoutForMapSample(long workoutId, int localSource, int sample) {
        db = LitePal.getDatabase();//getSugarDb().getDB();
        String str = "SELECT " + COLUMNS_FOR_MAP + " FROM trackpoint WHERE workoutid = ?";
        if (localSource == 2) {
            str = str + " order by sumwheel asc";
        }
        Cursor rawQuery = db.rawQuery(str, new String[]{String.valueOf(workoutId)});
        int count = rawQuery.getCount();
        if (count > sample) {
            rawQuery = db.rawQuery("SELECT " + COLUMNS_FOR_MAP + " FROM trackpoint WHERE workout_id = ? AND id % ? = 0", new String[]{String.valueOf(workoutId), String.valueOf(((count - 1) / sample) + 1)});
            count = rawQuery.getCount();
        }
        if (count < 0) {
            count = 0;
        }
        ArrayList<ITrackPoint> arrayList = new ArrayList<>(count);
        while (rawQuery.moveToNext()) {
            Trackpoint trackpoint = new Trackpoint();
            trackpoint.id = rawQuery.getLong(0);
            trackpoint.latitude = rawQuery.getDouble(1);
            trackpoint.longitude = rawQuery.getDouble(2);
            trackpoint.altitude = rawQuery.getDouble(3);
            trackpoint.pointType = rawQuery.getInt(4);
            trackpoint.speed = rawQuery.getDouble(5);
            if (CommonUtil.isPointValid(trackpoint)) {
                arrayList.add(trackpoint);
            }
        }
        rawQuery.close();
        return arrayList;
    }

    public static List<ITrackPoint> getByWorkoutForStartEndTimeSample(long workoutId, int localSource, long startTime, long endTime, int sample) {
        db = LitePal.getDatabase();//getSugarDb().getDB();
        String str = "SELECT " + COLUMNS_FOR_MAP + " FROM trackpoint WHERE workoutid = ? AND time > ? AND time <?";
        if (localSource == 2) {
            str = str + " order by sumWheel asc";
        }
        Cursor rawQuery = db.rawQuery(str, new String[]{String.valueOf(workoutId), String.valueOf(startTime), String.valueOf(endTime)});
        int count = rawQuery.getCount();
        if (count == 0) {
            startTime = (int) startTime;
            endTime = (int) endTime;
            rawQuery = db.rawQuery(str, new String[]{String.valueOf(workoutId), String.valueOf(startTime), String.valueOf(endTime)});
            count = rawQuery.getCount();
        }
        if (count > sample) {
            rawQuery = db.rawQuery("SELECT " + COLUMNS_FOR_MAP + " FROM trackpoint WHERE workout_id = ? AND time > ? AND time <? AND id % ? = 0", new String[]{String.valueOf(workoutId), String.valueOf(startTime), String.valueOf(endTime), String.valueOf(((count - 1) / sample) + 1)});
            count = rawQuery.getCount();
        }
        if (count < 0) {
            count = 0;
        }
        ArrayList<ITrackPoint> arrayList = new ArrayList<>(count);
        while (rawQuery.moveToNext()) {
            Trackpoint trackpoint = new Trackpoint();
            trackpoint.id = rawQuery.getLong(0);
            trackpoint.latitude = rawQuery.getDouble(1);
            trackpoint.longitude = rawQuery.getDouble(2);
            trackpoint.altitude = rawQuery.getDouble(3);
            trackpoint.pointType = rawQuery.getInt(4);
            trackpoint.speed = rawQuery.getDouble(5);
            trackpoint.time = rawQuery.getLong(6);
            if (CommonUtil.isPointValid(trackpoint)) {
                arrayList.add(trackpoint);
            }
        }
        rawQuery.close();
        return arrayList;
    }


    public static List<ITrackPoint> getByWorkoutForWatermark(long workoutId) {
        db = LitePal.getDatabase();//getSugarDb().getDB();
        Cursor rawQuery = db.rawQuery("SELECT id, latitude, longitude, altitude, altitudegps FROM trackpoint WHERE workoutid = ?", new String[]{String.valueOf(workoutId)});
        int count = rawQuery.getCount();
        if (count < 0) {
            count = 0;
        }
        ArrayList<ITrackPoint> arrayList = new ArrayList<>(count);
        while (rawQuery.moveToNext()) {
            Trackpoint trackpoint = new Trackpoint();
            trackpoint.id = rawQuery.getLong(0);
            trackpoint.latitude = rawQuery.getDouble(1);
            trackpoint.longitude = rawQuery.getDouble(2);
            trackpoint.altitude = rawQuery.getDouble(3);
            trackpoint.altitudeGps = rawQuery.getDouble(4);
            if (CommonUtil.isPointValid(trackpoint)) {
                arrayList.add(trackpoint);
            }
        }
        rawQuery.close();
        return arrayList;
    }

    public static List<ITrackPoint> getTrackpointsForChart(long workoutId) {
        SQLiteDatabase db2 = LitePal.getDatabase();//getSugarDb().getDB();
        db = db2;
        Cursor rawQuery = db2.rawQuery("SELECT " + COLUMNS_FOR_CHART + " FROM trackpoint WHERE workoutid = ?", new String[]{String.valueOf(workoutId)});
        int count = rawQuery.getCount();
        if (count < 0) {
            count = 0;
        }
        ArrayList<ITrackPoint> arrayList = new ArrayList<>(count);
        while (rawQuery.moveToNext()) {
            Trackpoint trackpoint = new Trackpoint();
            trackpoint.id = rawQuery.getLong(0);
            trackpoint.altitude = rawQuery.getDouble(1);
            trackpoint.speed = rawQuery.getFloat(2);
            trackpoint.cadence = rawQuery.getInt(3);
            trackpoint.heartrate = rawQuery.getInt(4);
            trackpoint.power = rawQuery.getDouble(5);
            trackpoint.latitude = rawQuery.getDouble(6);
            trackpoint.longitude = rawQuery.getDouble(7);
            trackpoint.sumWheel = rawQuery.getInt(8);
            trackpoint.time = rawQuery.getLong(9);
            arrayList.add(trackpoint);
        }
        rawQuery.close();
        return arrayList;
    }


    public static List<ITrackPoint> getTrackpointsForChartAbove0Speed(long workoutId) {
        SQLiteDatabase db2 = LitePal.getDatabase();//getSugarDb().getDB();
        db = db2;
        Cursor rawQuery = db2.rawQuery("SELECT " + COLUMNS_FOR_CHART + " FROM trackpoint WHERE workoutid = ? and speed > 0", new String[]{String.valueOf(workoutId)});
        int count = rawQuery.getCount();
        if (count < 0) {
            count = 0;
        }
        ArrayList<ITrackPoint> arrayList = new ArrayList<>(count);
        while (rawQuery.moveToNext()) {
            Trackpoint trackpoint = new Trackpoint();
            trackpoint.id = rawQuery.getLong(0);
            trackpoint.altitude = rawQuery.getDouble(1);
            trackpoint.speed = rawQuery.getFloat(2);
            trackpoint.cadence = rawQuery.getInt(3);
            trackpoint.heartrate = rawQuery.getInt(4);
            trackpoint.power = rawQuery.getDouble(5);
            trackpoint.latitude = rawQuery.getDouble(6);
            trackpoint.longitude = rawQuery.getDouble(7);
            trackpoint.sumWheel = rawQuery.getInt(8);
            if (CommonUtil.isPointValid(trackpoint.getLatitude(), trackpoint.getLongitude())) {
                arrayList.add(trackpoint);
            }
        }
        rawQuery.close();
        return arrayList;
    }

    public static List<ITrackPoint> getTrackpointsForChartAbove30Heartrate(long workoutId) {
        SQLiteDatabase db2 = LitePal.getDatabase();//SugarRecord.getSugarDb().getDB();
        db = db2;
        Cursor rawQuery = db2.rawQuery("SELECT " + COLUMNS_FOR_CHART + " FROM trackpoint WHERE workoutid = ? and heartrate > 30", new String[]{String.valueOf(workoutId)});
        int count = rawQuery.getCount();
        if (count < 0) {
            count = 0;
        }
        ArrayList<ITrackPoint> arrayList = new ArrayList<>(count);
        while (rawQuery.moveToNext()) {
            Trackpoint trackpoint = new Trackpoint();
            trackpoint.id = rawQuery.getLong(0);
            trackpoint.altitude = rawQuery.getDouble(1);
            trackpoint.speed = rawQuery.getFloat(2);
            trackpoint.cadence = rawQuery.getInt(3);
            trackpoint.heartrate = rawQuery.getInt(4);
            trackpoint.power = rawQuery.getDouble(5);
            trackpoint.latitude = rawQuery.getDouble(6);
            trackpoint.longitude = rawQuery.getDouble(7);
            trackpoint.sumWheel = rawQuery.getInt(8);
            if (CommonUtil.isPointValid(trackpoint.getLatitude(), trackpoint.getLongitude())) {
                arrayList.add(trackpoint);
            }
        }
        rawQuery.close();
        return arrayList;
    }

    public static long getCountByWorkout(long workoutId) {
        return WorkoutDatabaseHelper.countTackPoint(workoutId);
    }

    public static long getHeartrateCountByWorkout(long workoutId) {
        long count = LitePal.where("workoutId=? and heartrate>0", String.valueOf(workoutId)).count(Trackpoint.class);
        return count;

//        return Select.from(Trackpoint.class).where("workout_id = ? and heartrate > 0", new String[]{String.valueOf(j)}).count();
    }

    public static long getPowerCountByWorkout(long workoutId) {
        long count = LitePal.where("workoutId=? and power>0", String.valueOf(workoutId)).count(Trackpoint.class);
        return count;

//        return Select.from(Trackpoint.class).where("workoutId = ? and power > 0", new String[]{String.valueOf(j)}).count();
    }

    public static long getCadenceCountByWorkout(long workoutId) {

        long count = LitePal.where("workoutId=? and cadence>0", String.valueOf(workoutId)).count(Trackpoint.class);
        return count;

//        return Select.from(Trackpoint.class).where("workoutId = ? and cadence > 0", new String[]{String.valueOf(j)}).count();
    }

    public static long getCountInHeartrateSection(long workoutId, int min, int max) {
        if (min < 0) {
            min = 0;
        }
        if (max < 0) {
            long count = LitePal.where("workoutId=? and heartrate>?", String.valueOf(workoutId), String.valueOf(min)).count(Trackpoint.class);
            return count;

//            return Select.from(Trackpoint.class).where("workoutId = ? and heartrate > ?", new String[]{String.valueOf(j), String.valueOf(i)}).count();
        }


        long count = LitePal.where("workoutId=? and heartrate>? and heartrate<=?", String.valueOf(workoutId), String.valueOf(min), String.valueOf(max)).count(Trackpoint.class);
        return count;

//        return Select.from(Trackpoint.class).where("workoutId = ? and heartrate > ? and heartrate <= ?", new String[]{String.valueOf(j), String.valueOf(i), String.valueOf(i2)}).count();

    }

    public static long getCountInCadenceSection(long workoutId, int min, int max) {
        if (min < 0) {
            min = 0;
        }
        if (max < 0) {

            long count = LitePal.where("workoutId=? and cadence>?", String.valueOf(workoutId), String.valueOf(min)).count(Trackpoint.class);
            return count;

//            return Select.from(Trackpoint.class).where("workoutId = ? and cadence > ?", new String[]{String.valueOf(workoutId), String.valueOf(min)}).count();
        }


        long count = LitePal.where("workoutId=? and cadence>? and cadence <= ?", String.valueOf(workoutId), String.valueOf(min), String.valueOf(max)).count(Trackpoint.class);
        return count;

//        return Select.from(Trackpoint.class).where("workoutId = ? and cadence > ? and cadence <= ?", new String[]{String.valueOf(workoutId), String.valueOf(min), String.valueOf(max)}).count();

    }

    public static long getCadenceSumByWorkout(long workoutId) {
        Cursor rawQuery = LitePal.getDatabase().rawQuery("select sum(cadence) from trackpoint where workoutid = ?", new String[]{String.valueOf(workoutId)});
        long j = rawQuery.moveToFirst() ? rawQuery.getInt(0) : 0L;
        rawQuery.close();
        return j;
    }

    public static long getCountInPowerSection(long workoutId, int min, int max) {
        if (min < 0) {
            min = 0;
        }
        if (max < 0) {

            long count = LitePal.where("workoutId=? and power>?", String.valueOf(workoutId), String.valueOf(min)).count(Trackpoint.class);

            return count;

//            return Select.from(Trackpoint.class).where("workoutId = ? and power > ?", new String[]{String.valueOf(j), String.valueOf(i)}).count();
        }


        long count = LitePal.where("workoutId=? and power>? and power <= ?", String.valueOf(workoutId), String.valueOf(min), String.valueOf(max)).count(Trackpoint.class);
        return count;

//        return Select.from(Trackpoint.class).where("workoutId = ? and power > ? and power <= ?", new String[]{String.valueOf(j), String.valueOf(i), String.valueOf(i2)}).count();

    }

    //    @Deprecated
    public static void savePoints(final List<Trackpoint> trackpoints) {
//        SugarTransactionHelper.doInTansaction(new SugarTransactionHelper.Callback() {
//            @Override
//            public void manipulateInTransaction() {
        for (Trackpoint trackpoint : trackpoints) {
            trackpoint.save();
        }
//            }
//        });
    }

//    @Override
//    @Deprecated
//    public long save() {
//        long save = super.save();
//        Log.i("db", "track point [#" + save + "] saved ! workout: " + this.workoutId);
//        return save;
//    }

    @Override
    public int getSource() {
        return this.source;
    }

    public void setSource(int source) {
        this.source = source;
    }

    @Override
    public int getFlag() {
        return this.flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    @Override
    public int getSumCadence() {
        return this.sumCadence;
    }

    public void setSumCadence(int sumCadence) {
        this.sumCadence = sumCadence;
    }

    @Override
    public int getSumWheel() {
        return this.sumWheel;
    }

    public void setSumWheel(int sumWheel) {
        this.sumWheel = sumWheel;
    }

    @Override
    public int getCadence() {
        return this.cadence;
    }

    public void setCadence(int cadence) {
        this.cadence = cadence;
    }

    @Override
    public int getHeartrate() {
        return this.heartrate;
    }

    @Override
    public void setHeartrate(int heartrate) {
        this.heartrate = heartrate;
    }

    @Override
    public double getLatitude() {
        return this.latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    @Override
    public double getLongitude() {
        return this.longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    @Override
    public LatLng getLatLng() {
        return new LatLng(this.latitude, this.longitude);
    }

    @Override
    public double getAltitude() {
        return this.altitude;
    }

    public void setAltitude(double altitude) {
        this.altitude = altitude;
    }

    @Override
    public double getSpeed() {
        return this.speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    @Override
    public double getAccuracy() {
        return this.accuracy;
    }

    public void setAccuracy(double accuracy) {
        this.accuracy = accuracy;
    }

    @Override
    public double getBearing() {
        return this.bearing;
    }

    public void setBearing(double bearing) {
        this.bearing = bearing;
    }

    @Override
    public long getTime() {
        return this.time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    @Override
    public long getWorkoutId() {
        return this.workoutId;
    }

    public void setWorkoutId(long workoutId) {
        this.workoutId = workoutId;
    }

    @Override
    public int getWheelRpm() {
        return this.wheelRpm;
    }

    public void setWheelRpm(int wheelRpm) {
        this.wheelRpm = wheelRpm;
    }

    @Override
    public double getPressure() {
        return this.pressure;
    }

    public void setPressure(double pressure) {
        this.pressure = pressure;
    }

    @Override
    public double getTemperature() {
        return this.temperature;
    }

    public void setTemperature(double temperature) {
        this.temperature = temperature;
    }

    @Override
    public double getElevation() {
        return this.elevation;
    }

    public void setElevation(double elevation) {
        this.elevation = elevation;
    }

    @Override
    public double getGrade() {
        return this.grade;
    }

    public void setGrade(double grade) {
        this.grade = grade;
    }

    @Override
    public double getPower() {
//        Log.d(TAG, "getPower this.power=" + this.power);
        return this.power;
    }

    @Override
    public void setPower(double power) {
        this.power = power;
//        Log.d(TAG, "setPower this.power=" + this.power);
    }

    public String getPowerExt() {
        return this.powerExt;
    }

    public void setPowerExt(String powerExt) {
        this.powerExt = powerExt;
    }

    public double getDistanceEach() {
        return this.distanceEach;
    }

    public void setDistanceEach(double distanceEach) {
        this.distanceEach = distanceEach;
    }

    public long getDurationEach() {
        return this.durationEach;
    }

    public void setDurationEach(long durationEach) {
        this.durationEach = durationEach;
    }

    @Override
    public int getPointType() {
        return this.pointType;
    }

    public void setPointType(int pointType) {
        this.pointType = pointType;
    }

    public double getSpeedGps() {
        return this.speedGps;
    }

    public void setSpeedGps(double speedGps) {
        this.speedGps = speedGps;
    }

    public double getSpeedCa() {
        return this.speedCa;
    }

    public void setSpeedCa(double speedCa) {
        this.speedCa = speedCa;
    }

    @Override
    public double getAltitudeGps() {
        return this.altitudeGps;
    }

    public void setAltitudeGps(double altitudeGps) {
        this.altitudeGps = altitudeGps;
    }

    public double getAltitudeCa() {
        return this.altitudeCa;
    }

    public void setAltitudeCa(double altitudeCa) {
        this.altitudeCa = altitudeCa;
    }

    @Override
    public double getDistance() {
        return this.distance;
    }

    public void setDistance(double distance) {
        this.distance = distance;
    }

    @Override
    public long getDuration() {
        return this.duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public double getElevationGain() {
        return this.elevationGain;
    }

    public void setElevationGain(double elevationGain) {
        this.elevationGain = elevationGain;
    }

    public double getElevationLoss() {
        return this.elevationLoss;
    }

    public void setElevationLoss(double elevationLoss) {
        this.elevationLoss = elevationLoss;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeDouble(this.accuracy);
        parcel.writeDouble(this.altitude);
        parcel.writeDouble(this.latitude);
        parcel.writeDouble(this.longitude);
        parcel.writeDouble(this.bearing);
        parcel.writeDouble(this.speed);
        parcel.writeLong(this.time);
        parcel.writeLong(this.workoutId);
        parcel.writeInt(this.cadence);
        parcel.writeInt(this.heartrate);
        parcel.writeInt(this.wheelRpm);
        parcel.writeInt(this.sumCadence);
        parcel.writeInt(this.sumWheel);
        parcel.writeInt(this.source);
        parcel.writeInt(this.flag);
        parcel.writeDouble(this.pressure);
        parcel.writeDouble(this.temperature);
        parcel.writeDouble(this.elevation);
        parcel.writeDouble(this.grade);
        parcel.writeDouble(this.power);
        parcel.writeString(this.powerExt);
        parcel.writeDouble(this.distanceEach);
        parcel.writeLong(this.durationEach);
        parcel.writeInt(this.pointType);
        parcel.writeDouble(this.speedGps);
        parcel.writeDouble(this.speedCa);
        parcel.writeDouble(this.altitudeGps);
        parcel.writeDouble(this.altitudeCa);
        parcel.writeDouble(this.distance);
        parcel.writeLong(this.duration);
        parcel.writeDouble(this.elevationGain);
        parcel.writeDouble(this.elevationLoss);
    }

    protected Trackpoint(Parcel parcel) {
        this.latitude = 90.0d;
        this.longitude = 180.0d;
        this.source = 1;
        this.altitudeCa = -2.147483648E9d;
        this.accuracy = parcel.readDouble();
        this.altitude = parcel.readDouble();
        this.latitude = parcel.readDouble();
        this.longitude = parcel.readDouble();
        this.bearing = parcel.readDouble();
        this.speed = parcel.readDouble();
        this.time = parcel.readLong();
        this.workoutId = parcel.readLong();
        this.cadence = parcel.readInt();
        this.heartrate = parcel.readInt();
        this.wheelRpm = parcel.readInt();
        this.sumCadence = parcel.readInt();
        this.sumWheel = parcel.readInt();
        this.source = parcel.readInt();
        this.flag = parcel.readInt();
        this.pressure = parcel.readDouble();
        this.temperature = parcel.readDouble();
        this.elevation = parcel.readDouble();
        this.grade = parcel.readDouble();
        this.power = parcel.readDouble();
        this.powerExt = parcel.readString();
        this.distanceEach = parcel.readDouble();
        this.durationEach = parcel.readLong();
        this.pointType = parcel.readInt();
        this.speedGps = parcel.readDouble();
        this.speedCa = parcel.readDouble();
        this.altitudeGps = parcel.readDouble();
        this.altitudeCa = parcel.readDouble();
        this.distance = parcel.readDouble();
        this.duration = parcel.readLong();
        this.elevationGain = parcel.readDouble();
        this.elevationLoss = parcel.readDouble();
    }
}